<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 双向绑定checkbox的数组
      roleIds: [],
      // 用户角色数据
      rolesList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 实现分配角色权限
    async doAdd() {
      const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
      console.log('添加角色权限', res)
      this.$emit('success')
    },
    // 添加角色权限 确认按钮
    hSubmit() {
      this.doAdd()
    },
    // 获取角色列表
    async loadRoles() {
      try {
        const res = await getRoles()
        console.log('getRoles', res)
        // 保存数据
        this.rolesList = res.data.rows
        // 获取用户信息储存的id 双向绑定 checkbox
        const info = await getUserDetail(this.curId)
        console.log('info', info)
        // 双向绑定 checkbox
        this.roleIds = info.data.roleIds
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
